lv.calendar({
    //autoRender: true,  //自动渲染日历
    //trigger: ".food_time_box",  //触发的位置
    todayTitle: renderTodayTitle(),
    trigger: "#showVisitTimeInput",  //触发的位置
    triggerEvent: "click",
    template: "big",
    zIndex:"50",
    clickDocumentHide:false,
    showPriceTip:false,
    //点击选择日期后的回调函数 默认返回值: calendar对象
    selectDateCallback: function (self) {
        var that = this,
            weekArr = ['日','一','二','三','四','五','六'];
        var $timeBox = $(that.options.trigger);

        //设置星期
        self.$trigger.siblings('.time_week').html('周'+weekArr[that.now.getDay()]);
        //触发校验
        self.$trigger.blur();

        //收起高度
        self.$trigger.parents('.orderDl').find('.food_time_dd').height(0);

        $("#visitTimeInput").val($('#showVisitTimeInput').val());
        $('.ui-calendar').remove();
        calendarConfirmCallback.invoke();
        $(this).removeClass('input_red');
        //更改商品列表日期
        $(".ticketLi").each(function(index){
            if(!$(this).hasClass("main")){
                $(this).find(".orderDl .c3").html(self.now.toLocaleDateString().split('/').join('-'));
            }
        });

        //如果有其他商品，那么显示添加更多门票按钮
        if($('.addTicket-table .ptList').length > 0){
            $('.js_add_ticket').show();
        }
    },
    completeCallback:function (self) {
        self.$trigger.parents('.orderDl').find('.food_time_dd').height(355);
        var date=$("#visitTimeInput").val();
        var uiCalendar = $(".ui-calendar");
        uiCalendar.find("td[date-map]").children("div").removeClass("today");
        uiCalendar.find("td[date-map='"+date+"']").children("div").addClass("today");
    },
    sourceFn: function () {
        var that = this;
        that.loading();
        var suppGoodsId;
        var distributorId = $("#distributorId").val();
        var visitTime=$('#visitTimeInput').val();

        var js_input = $(".js_input");
        js_input.each(function(){
            var goodsId=$(this).attr("goodsId");
            var mainItem=$(this).attr("mainItem");
            if(mainItem=="true"){
                suppGoodsId=goodsId;
                return;
            }
        });

        var productId;
        js_input.each(function(){
            var id=$(this).attr("productId");
            var mainItem=$(this).attr("mainItem");
            if(mainItem=="true"){
                productId=id;
                return;
            }
        });

        var productFlag = $("#productFlag").val();
        var url = productFlag?"getCombTicketTimePrice":"getTicketTimePrice";
        var data;
        if(productFlag){
            data = {
                productId:productId,
                distributorId:distributorId
            };
        }else{
            data = {
                suppGoodsId:suppGoodsId,
                visitTime:visitTime,
                distributorId:distributorId
            };
        }
        $.ajax({
            url: "/scenic_front/book/ticket/"+url+".do",
            data:data,
            dataType: "json",
            async: true
        }).done(function (data) {
            //完成
            setDate(that,data);
            that.loaded();
        }).fail(function (error) {
            //TODO 错误处理
        })
    } //填充时间价格表
});

/**
 * 设置显示
 * @param data Ajax返回值
 * @returns {boolean}
 */
function setDate(that,data) {
    var self = that;

    /**
     * 退化的情况，无任何返回值
     */
    if (!data) {
        return false;
    }

    var $allTd = self.wrap.find('td[data-date-map]');  //所有的日历单元格
    $allTd.children().addClass("caldisabled");  //先禁用所有的日历日期

    function getStoreHTML(inventory,oversold) {
        var html = "";
        if(oversold == 'Y'){
            return html;
        }

        if (typeof inventory == "number" && inventory <= 0) {
            html = "售罄";
        } else if (typeof inventory == "number" && inventory < 10) {
            html = "余" + inventory
        }
        return html;
    }


    //对json对象进行迭代处理
    data.attributes.timePriceList.forEach(function (row) {

        var jsonDateStr = row.specDateStr;
        var date = lv.calendar.getDateFromFormattedString(jsonDateStr, self.options.dateFormat);

        var dateStr = lv.calendar.dateFormat(date, self.options.dateFormat);
        var price = row.subZeroPriceYuan;
        var inventory = row.stock;
        var oversold = row.oversellFlag;
        var stockFlag = row.stockFlag;
        //var lineRouteName = row.lineRouteName;

        var $td = self.wrap.find('td[data-date-map=' + dateStr + ']');
        if ($td) {

            var $calActive = $td.find(".calactive");
            if (stockFlag == "N" || oversold == "Y" || (typeof inventory !== "number" && inventory > 0)) {
                //设置可以点击选择当前日期
                $td.children().removeClass("caldisabled").removeClass("nodate");
            } else if (inventory <= 0) {  //库存小1的时候 不可点击 不可购买 不显示
                $td.find(".calinfo").addClass("sellout");
                $td.children().removeClass("caldate").addClass("nodate");
                return false;
            } else if (inventory <= 3 && inventory > 0) {
                $td.children().removeClass("caldisabled").removeClass("nodate");
                $td.find(".calinfo").addClass("sellouting");
            } else {
                $td.children().removeClass("caldisabled").removeClass("nodate");
            }

            //促销等元素的显示位置
            /*if (lineRouteName) {
                var $lineRouteName = $('<div class="calroute">' + lineRouteName + '</div>');
                $calActive.append($lineRouteName);
            }*/

            //显示库存
            $td.find(".calinfo").html(getStoreHTML(inventory,oversold));

            //显示价格
            $td.find(".calprice").html('<i>&yen;</i><em>' + price + '</em>');
        }
    });
}

function renderTodayTitle() {
    var prodProductType = $('#prodProductType').val();
    if (prodProductType === 'FOREIGNLINE') {
        var date = new Date().getDate();
        return date < 10 ? '0' + date : date;
    }
    return '今天';
}